﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;

namespace DevHub.WindowsApp.Dialogs;

public partial class IconDialog : UserControl
{
    private readonly Symbol _symbol;

    public IconDialog(Symbol symbol)
    {
        InitializeComponent();
        _symbol = symbol;
    }

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);
        SymbolIcon.Symbol = _symbol;
        Block.Text = _symbol.ToString();
        XamlCodeBlock.Text = $"<controls:SymbolIcon Symbol=\"{Block.Text}\" />";
        CSCodeBlock.Text = $"var icon = new SymbolIcon(){{Symbol = {Block.Text} }};";
    }

    private void NameCopyClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(Block.Text)) return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(Block.Text);
    }

    private void XamlCopyClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(XamlCodeBlock.Text)) return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(XamlCodeBlock.Text);
    }

    private void CsharpCopyClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(CSCodeBlock.Text)) return;
        var top = TopLevel.GetTopLevel(this);
        top?.Clipboard?.SetTextAsync(CSCodeBlock.Text);
    }
}